#include "gamemap.h"
#include "managegame.h"
#include "enemy.h"
#include "Console.h"
#include <stdio.h>


void GameMap::init(point a_size, point a_tilesize, char * a_filename)
{
	AssetIndex = g_app.getGraphics()->AddAsset(a_filename, 0);
	m_size = a_size;
	m_tilesize = a_tilesize;
	m_map = new MapTile * [(int)m_size.y];
	for(int y = 0; y < m_size.y; ++y)
	{
		m_map[y] = new MapTile[(int)m_size.x];
	}
}

void GameMap::addToDisplayList()
{
	g_app.getGraphics()->AddObject(this, AssetIndex, 0);
}

bool isEnemyColliding(BaseObject * e1, BaseObject * e2)
{
	point tilesize = g_app.getMap()->getTileSize();
	Quad qe1(
		(int)e1->getLoc().x,	(int)e1->getLoc().y, 
		(int)tilesize.x,		(int)tilesize.y);
	Quad qe2(
		(int)e2->getLoc().x,	(int)e2->getLoc().y, 
		(int)tilesize.x,		(int)tilesize.y);
	return qe1.isColliding(qe2);
}

void GameMap::update(int a_ms)
{
//	char buffer[256];
//	sprintf_s(buffer, 256, "%d\n", a_ms);
//	g_console->Print(buffer);
	BaseObject * e1, * e2;
	for(int a = 0; a < m_mobs.size(); ++a)
	{
		e1 = m_mobs.get(a);
		for(int b = a+1; b < m_mobs.size(); ++b)
		{
			e2 = m_mobs.get(b);
			if(isEnemyColliding(e1, e2))
			{
				char buffer[256];
				sprintf_s(buffer, 256, "BEEP!\n");
				g_console->Print(buffer);
				g_app.getSound()->playSound(1);
				e1->collideWith(e2);
				e2->collideWith(e1);
			}
		}
	}

	for(int i = 0; i < m_mobs.size(); ++i)
	{
		m_mobs.get(i)->update(a_ms);
	}
}
